clear all
set type double
import delimited using ../Data/Mongolia/bid_06jul22.csv, clear encoding("utf-8") varnames(1)
gen o_index = _n

gen o_win = (mode == "Шалгарсан")
gen o_bidder = тендерторолцогчийннэр
gen o_page = page
gen o_no = no
gen o_table_index = table_index
gen o_date = dateofbidopenning
gen o_budget = approvedbudget_outline
gen o_bid = саналболгосонүнэ
gen o_discount = хөнгөлөлт
gen o_link = link
gen o_nameofbidding = nameofbidding
gen o_nameofprocuringentity = nameofprocuringentity
gen o_procedureofprocurement = procedureofprocurement

*tab typeofbidding
cap drop o_contract
gen o_contract = typeofbidding
replace o_contract = ustrregexra(o_contract,"Ажил","Work")
replace o_contract = ustrregexra(o_contract,"Бараа","Goods")
replace o_contract = ustrregexra(o_contract,"Зөвлөх үйлчилгээ","Consulting services")
replace o_contract = ustrregexra(o_contract, "Зөвлөхөөс бусад *үйлчилгээ","Services other than consulting")
* firm id
cap tempvar tempv
cap drop o_firm_id
gen `tempv' = тендерторолцогчийннэр if ustrregexm(тендерторолцогчийннэр, "AAAA")
gen o_firm_id = ustrregexra(`tempv', "(.+)[^0-9]{1}AAAA.*","$1")
order o_firm_id тендерторолцогчийннэр
drop `tempv'

drop if o_table_index == 0
drop if o_bidder == o_bid
order o_*

destring o_bid, replace

replace o_budget = ustrregexra(o_budget, "[₮,]","")
destring o_budget, replace
compress
*format o_bid %40s
format o_bidder %60s

gen o_bid_index = ustrregexra(unnamed01,"\.","")
destring o_bid_index, replace

gen o_contract_id = (link != link[_n-1])
replace o_contract_id = o_contract_id + o_contract_id[_n-1] if _n > 1
order o_contract_id

sort o_index
order o*
keep o*


qui ds
foreach c in `r(varlist)'{
    di "`c'"
	local d = ustrregexra("`c'","o_","")
	rename `c' `d'
}


/*################################################################*/
* item is the auction index, taking into account multiple auctions in a single project *
cap drop item
gen item = (bid_index <= bid_index[_n-1])
order item bid_index
*gen item = temp
replace item = item + item[_n-1] if _n > 1

cap drop auction_id
gen auction_id = strofreal(contract_id) + "A" + strofreal(item,"%09.0f")
order auction_id

sort auction_id bid bidder index
duplicates tag auction_id bid bidder, gen(d0)
by auction_id bid bidder: gen d0n = _n
order d0*
keep if d0n == 1
duplicates report auction_id bid bidder

sort auction_id bid bidder
by auction_id: gen bid_rank = _n if bid < .

bys auction_id: gen N_bidders = _N

* 
cap drop N_bids
by auction_id: egen N_bids = total(bid < .)
cap drop temp
gen temp = 1 if N_bids ==0 /*delete auctions in which all bids are missing*/
by auction_id: egen FLAG = max(temp)
replace temp = 1 if win == 1 & bid == . /* delete auctions whose winning bid is missing*/
replace FLAG = 0 if FLAG == .
drop temp

bys auction_id: egen N_winners = total(win) if FLAG != 1

sort auction_id bid_index
drop if bidder == bidder[_n-1] & N_winners > 1 & win == 1 & bid == bid[_n-1]
drop N_winners
bys auction_id: egen N_winners = total(win) if FLAG != 1

/* by bidder */
bys firm_id: gen N_participations = _N if FLAG != 1


tempvar v
forvalues x = 1(1)4{
    gen `v' = bid if bid_rank == `x' & FLAG != 1
	bys auction_id: egen rank`x'_bid = max(`v') if FLAG != 1
	drop `v'
}

forvalues x = 1(1)3{
	cap drop delta`x'
	local y = `x' + 1
    gen delta`x' = (bid - rank`x'_bid)/bid if bid_rank > `x' & bid_rank < . &FLAG != 1
	replace delta`x' = (bid - rank`y'_bid)/bid if bid_rank == `x' & FLAG != 1
}

gen `v' = bid_rank if win == 1 & FLAG != 1
bys auction_id: egen win_bid_rank = max(`v') if  FLAG != 1
replace `v' = bid_rank if win_bid_rank <= bid_rank & FLAG != 1
bys auction_id: egen bid_rank_win = rank(`v') if FLAG != 1, u
*br auction_id bid bid_rank* win_bid_rank win `v'
drop `v'

forvalues x = 1(1)4{
    gen `v' = bid if bid_rank_win == `x'& FLAG != 1
	bys auction_id: egen rank`x'_bid_win = max(`v')	if FLAG != 1
	drop `v'
}
forvalues x = 1(1)3{
	cap drop delta`x'w
	local y = `x' + 1
    gen delta`x'w = (bid - rank`x'_bid_win)/bid if bid_rank_win > `x' ///
		& bid_rank_win < . & FLAG != 1
	replace delta`x'w = (bid - rank`y'_bid_win)/bid if bid_rank_win == `x' & FLAG != 1
}


gen winbid = rank1_bid_win if FLAG != 1

keep if FLAG != 1

rename win winner
rename contract_id contract_id_original
egen contract_id = group(auction_id)
gen date0 = ustrregexra(date, "[0-9]{2}:[0-9]{2}","")
cap drop deadline_bidding
gen bid_date = date(date0, "YMD")
format bid_date %td
destring firm_id, replace ignore("""")

************* backlog construction module *****************
do backlog_construction
backlog_construction "" ""
***********************************************************
************* fowardlog construction module *****************
do forwardlog_construction.do
fowardlog_construction "" "" 
***********************************************************
save ../Data/mongolia/sample_mongolia_30jun22.dta, replace /*fix sample*/









